package com.zzz.robot201.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.NetworkUtils;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzz.robot201.R;
import com.zzz.robot201.app.base.BaseViewActivity;
import com.zzz.robot201.di.component.DaggerDevicesComponent;
import com.zzz.robot201.mvp.contract.DevicesContract;
import com.zzz.robot201.mvp.model.api.bean.BaseBean;
import com.zzz.robot201.mvp.presenter.DevicesPresenter;
import com.zzz.robot201.utils.LogUtils;
import com.zzz.robot201.utils.ToastUtil;
import com.zzz.robot201.utils.UIUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class DevicesActivity extends BaseViewActivity<DevicesPresenter> implements DevicesContract.View, QRCodeView.Delegate {

    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_scan)
    Button btnScan;
    @BindView(R.id.rl_sacn)
    RelativeLayout rlSacn;
    @BindView(R.id.btn_cancle)
    Button btnCancle;
    @BindView(R.id.btn_bind)
    Button btnBind;
    @BindView(R.id.rl_devices)
    RelativeLayout rlDevices;
    private QRCodeView mQRCodeView;
    private String deviceCode;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerDevicesComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_devices; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setTitle(tvTitle, UIUtils.getString(R.string.title_equipment_distinguish));
        mQRCodeView = (ZBarView) findViewById(R.id.zbarview);
        mQRCodeView.setDelegate(this);
    }

    @Override
    public void showLoading() {
        showBaseLoading();
    }

    @Override
    public void hideLoading() {
        hideBaseLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastUtil.showToast(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onNetworkDisconnected() {
        super.onNetworkDisconnected();
        ToastUtil.showToast("网络断开");
    }

    @Override
    protected void onNetworkConnected(NetworkUtils.NetworkType networkType, String ssid) {
        super.onNetworkConnected(networkType, ssid);
        ToastUtil.showToast("网络类型：" + networkType.name() + "---Wifi名：" + ssid);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_cancle, R.id.btn_bind, R.id.btn_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancle:
                setTitle(tvTitle, UIUtils.getString(R.string.title_equipment_distinguish));
                rlSacn.setVisibility(View.VISIBLE);
                rlDevices.setVisibility(View.GONE);
                break;
            case R.id.btn_back:
                killMyself();
                break;
            case R.id.btn_bind:
                if (mPresenter != null) mPresenter.getScanBind("00001");
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();//打开相机
        mQRCodeView.showScanRect();//显示扫描框
        mQRCodeView.startSpot();//开始识别二维码
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        // 扫描完成手机振动
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        setTitle(tvTitle, UIUtils.getString(R.string.title_equipment));
        rlSacn.setVisibility(View.GONE);
        rlDevices.setVisibility(View.VISIBLE);
        mQRCodeView.startSpot();
        vibrate();
        deviceCode=result;
        LogUtils.logGGQ(result);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }

    @Override
    public void getScanBind(BaseBean baseBean) {
        ToastUtil.showToast(baseBean.getMsg());
        startActivity(new Intent(this, HomeActivity.class));
    }
}
